<template>
	<el-dialog
	  :title="isUpgrade ? '升级标准' : !isUpdate ? '新增标准' : '修改标准'"
	  :visible.sync="visible"
	  :before-close="close"
    append-to-body
	  :close-on-click-modal="false"
	  width="70%">

    <el-row v-if="isUpgrade" :gutter="12">
      <el-col :span="5" :offset="5">
        <el-card shadow="hover">
          <div class="old-num">{{childDataForm.oldNum}}</div>
        </el-card>
      </el-col>
      <el-col :span="2">
        <div class="to">
          <i class="el-icon-right"></i>
        </div>
      </el-col>
      <el-col :span="5">
        <el-card shadow="hover">
          <div class="new-num">{{dataForm.insideInspectionStandardNum}}</div>
        </el-card>
      </el-col>
    </el-row>

	  <el-form size="medium" label-position="top">

      <template v-if="isUpgrade || invalidateBtnVisible">
        <el-form-item>
          <el-switch
            v-model="dataForm.isWaitTakeEffect"
            @change="resetTet"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-text="isUpgrade ? '立即生效' : '已生效'"
            :inactive-text="isUpgrade ? '等待生效' : '失效,重新等待生效'"
            :active-value="1"
            :inactive-value="0">
          </el-switch>
        </el-form-item>
      </template>

      <template v-if="dataForm.isWaitTakeEffect == 0">
        <el-form-item label="生效时间">
          <el-date-picker
            v-model="dataForm.takeEffectTime"
            type="date"
            placeholder="生效时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="{
              disabledDate(time) {
                return time.getTime() < Date.now()
              }
            }"
            style="width: 100%">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="(或)生效批号" v-if="isZpOrCp">
          <el-input v-model="dataForm.effectiveBatchNum" @change="setEffectiveBatchNum" placeholder="生效批号" />
        </el-form-item>
      </template>

	  	<el-form-item prop="insideInspectionStandardNum" label="内部标准号">
        <el-input v-model="dataForm.insideInspectionStandardNum" placeholder="内部标准号"></el-input>
      </el-form-item>


	  <el-form-item prop="productGmpLike" label="物料编码关键字(取物料编码前缀)">
	    <el-input v-model="dataForm.productGmpLike" placeholder="物料编码关键字(取物料编码前缀)"></el-input>
	  </el-form-item>


      <el-form-item prop="nationInspectionStandardNum" label="国家标准号">
        <el-input v-model="dataForm.nationInspectionStandardNum" @change="dataForm.accordingToNation = dataForm.nationInspectionStandardNum" placeholder="国家标准号"></el-input>
      </el-form-item>

      <el-form-item prop="accordingToInside" label="内部检验依据">
        <el-input v-model="dataForm.accordingToInside" placeholder="内部检验依据"></el-input>
      </el-form-item>

      <el-form-item prop="accordingToNation" label="国家检验依据">
        <el-input v-model="dataForm.accordingToNation" placeholder="国家检验依据"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button v-if="!isUpgrade" type="primary" class="pan-btn primary-btn" size="small" @click="tableEdit">表格内编辑</el-button>
      	<el-tag
          v-for="(item,itemIndex) in items"
          :key="itemIndex"
				  closable
				  :disable-transitions="false"
				  @close="handleClose(itemIndex)">
				  <span style="cursor: pointer;" @click="editItem(itemIndex)">{{item.inspectionItemName}}</span>
				</el-tag>
				<el-button class="button-new-tag" size="small" @click="editItem('add')">+ 检验项</el-button>
      </el-form-item>

      <standard-table-edit
        v-if="tableEditVisible"
        @close="closeInnerTable"
        @getItems="getItems"
        :childDataForm="grandDataForm"/>

      <el-divider></el-divider>

      <el-form-item prop="specialValue" label="额外的结论">
        <el-input v-model="dataForm.specialValue" placeholder="额外的结论"></el-input>
      </el-form-item>

      <el-form-item v-if="showSpecialItems">
      	<el-tag
          v-for="(item,itemIndex) in specialItems"
          :key="itemIndex"
      	  closable
      	  :disable-transitions="false"
      	  @close="handleCloseSpecial(itemIndex)">
      	  <span style="cursor: pointer;" @click="editItemSpecial(itemIndex)">{{item.specialItemName}}</span>
      	</el-tag>
      	<el-button class="button-new-tag" size="small" @click="editItemSpecial('add')">+ 检验项</el-button>
      </el-form-item>

	  </el-form>

    <el-dialog
      width="50%"
      title="添加检验项"
      :visible.sync="addItemSpecialVisible"
      :before-close="closeInnerSpecial"
    	:close-on-click-modal="false"
      append-to-body>
      <el-form label-position="top" size="small">
        <el-form-item label="检验项分类">
        	<el-input v-model="newItemSpecial.specialItemCategory" placeholder="检验项分类" />
        </el-form-item>
        <el-form-item label="检验项名称">
        	<el-input v-model="newItemSpecial.specialItemName" placeholder="检验项名称" />
        </el-form-item>
        <el-form-item label="检验标准值">
        	<el-input v-model="newItemSpecial.specialItemValue" placeholder="检验标准值" />
        </el-form-item>
        <el-form-item label="内置检验结果">
        	<el-input v-model="newItemSpecial.specialItemConclusion" placeholder="内置检验结果" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" class="pan-btn primary-btn" @click="newItemSpecialSubmit">确 定</el-button>
      </span>
    </el-dialog>

	  <el-dialog
      width="50%"
      title="添加检验项"
      :visible.sync="addItemVisible"
      :before-close="closeInner"
	  	:close-on-click-modal="false"
      append-to-body>
      <el-form label-position="top" size="small">

      	<el-form-item prop="inspectionItemName" label="检验项名称">
        	<el-input v-model="newItem.inspectionItemName" @change="setPrintItemName" placeholder="检验项名称"></el-input>
      	</el-form-item>

        <el-form-item prop="isPrint" label="是否用于打印">
          <el-switch
            v-model="newItem.isPrint"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="是"
            inactive-text="否"
            :active-value="0"
            :inactive-value="1">
          </el-switch>
        </el-form-item>

        <el-form-item prop="printItemName" label="打印别名">
        	<el-input v-model="newItem.printItemName" placeholder="打印别名"></el-input>
        </el-form-item>

        <el-form-item prop="nationInspectionItemValue" label="国家标准值">
        	<el-input v-model="nationInspectionItemValueShow" placeholder="国家标准值"></el-input>
        </el-form-item>

      	<el-form-item prop="insideInspectionItemValue" label="内部标准值">
        	<el-input v-model="insideInspectionItemValueShow" placeholder="内部标准值"></el-input>
      	</el-form-item>

        <el-form-item prop="haveFormulaValue" label="标准值是否包含化学式">
          <el-switch
            v-model="newItem.haveFormulaValue"
            @change="resetFormulaSpec('insideAndNation')"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="包含"
            inactive-text="不包含"
            :active-value="0"
            :inactive-value="1">
          </el-switch>
        </el-form-item>

        <el-form-item v-if="newItem.haveFormulaValue == 0" prop="haveFormulaValue2" label="请选中化学式">
          <div v-if="nationInspectionItemValueShow != null && nationInspectionItemValueShow != ''">
            <div>
              <el-tooltip effect="dark" content="重置" placement="top">
                <i class="el-icon-refresh point" @click="resetFormulaSpec('nation')"></i>
              </el-tooltip>
            </div>
            <div style="color: #D43F3A;">
              <span>国家标准值：</span>
              <span @mouseup="selectFormula('nation')" v-html="newItem.nationInspectionItemValue"></span>
            </div>
          </div>
          <div v-if="insideInspectionItemValueShow != null && insideInspectionItemValueShow != ''">
            <div>
              <el-tooltip effect="dark" content="重置" placement="top">
                <i class="el-icon-refresh point" @click="resetFormulaSpec('inside')"></i>
              </el-tooltip>
            </div>
            <div style="color: #D43F3A;">
              <span>内部标准值：</span>
              <span @mouseup="selectFormula('inside')" v-html="newItem.insideInspectionItemValue"></span>
            </div>
          </div>
        </el-form-item>

        <el-form-item prop="isNumber" label="检验结果是否为数值">
          <el-switch
            v-model="newItem.isNumber"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="是"
            inactive-text="否"
            :active-value="0"
            :inactive-value="1">
          </el-switch>
        </el-form-item>

<!--        <el-form-item prop="isHalfYearTest" label="是否为每半年检一次">
          <el-switch
            v-model="newItem.isHalfYearTest"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="是"
            inactive-text="否"
            :active-value="0"
            :inactive-value="1">
          </el-switch>
        </el-form-item> -->

        <el-form-item prop="printValue" label="标准规定打印显示配置">
          <el-switch
            v-model="newItem.printValue"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="显示为'应符合规定。'"
            inactive-text="原本标准"
            :active-value="1"
            :inactive-value="0">
          </el-switch>
        </el-form-item>

        <el-form-item prop="printConfig" label="检验结果打印显示配置">
          <el-switch
            v-model="newItem.printConfig"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="显示为系统内置结果(符合/不符合规定)"
            inactive-text="原本结果"
            :active-value="1"
            :inactive-value="0">
          </el-switch>
        </el-form-item>

<!--        <el-form-item prop="manHourOne" label="标准工时1">
        	<el-input v-model="newItem.manHourOne" placeholder="标准工时1">
            <template slot="append">小时</template>
          </el-input>
        </el-form-item> -->

<!--        <el-form-item prop="manHourTwo" label="标准工时2">
        	<el-input v-model="newItem.manHourTwo" placeholder="标准工时2">
            <template slot="append">小时</template>
          </el-input>
        </el-form-item> -->

      	<el-form-item prop="insideInspectionItemDownValue" label="内部标准下限值">
        	<el-input v-model="newItem.insideInspectionItemDownValue" placeholder="内部标准下限值"></el-input>
      	</el-form-item>

      	<el-form-item prop="insideInspectionItemUpValue" label="内部标准上限值">
        	<el-input v-model="newItem.insideInspectionItemUpValue" placeholder="内部标准上限值"></el-input>
      	</el-form-item>

      	<el-form-item prop="insideInspectionItemValueUnit" label="内部标准上下限值单位">
        	<el-input v-model="newItem.insideInspectionItemValueUnit" placeholder="内部标准上下限值单位"></el-input>
      	</el-form-item>

      	<el-form-item prop="inspectionGroupId" label="所属检验组">
	        <el-select v-model="newItem.inspectionGroupId" filterable v-if="groupList.length" placeholder="所属检验组" style="width: 100%">
	          <el-option
	            v-for="group in groupList"
	            :key="group.inspectionGroupId"
	            :label="group.inspectionGroupName"
	            :value="group.inspectionGroupId">
	          </el-option>
	        </el-select>
	      </el-form-item>

	      <el-form-item prop="inspectionCategoryId" label="所属类别">
	        <el-select v-model="newItem.inspectionCategoryId" filterable v-if="categoryList.length" placeholder="所属类别" style="width: 100%">
	          <el-option
	            v-for="category in categoryList"
	            :key="category.inspectionCategoryId"
	            :label="category.inspectionCategoryName"
	            :value="category.inspectionCategoryId">
	          </el-option>
	        </el-select>
	      </el-form-item>

     <!--   <el-form-item v-if="newItem.inspectionGroupId != null && newItem.inspectionGroupId != ''" prop="waterQuantityId" label="水检验类别">
          <el-select v-model="newItem.waterQuantityId" @change="setWq" clearable filterable v-if="categoryList.length" placeholder="若不是水的项目则为空" style="width: 100%">
            <el-option
              v-for="wq in waterQuantityList.filter(v => {return newItem.inspectionGroupId == v.inspectionGroupId})"
              :key="wq.waterQuantityId"
              :label="wq.waterQuantityName"
              :value="wq.waterQuantityId">
            </el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
		    <el-button type="primary" class="pan-btn primary-btn" @click="newItemSubmit">确 定</el-button>
	  	</span>
    </el-dialog>

    <span slot="footer" class="dialog-footer">
	    <el-button @click="close">取 消</el-button>
	    <el-button type="primary" class="pan-btn primary-btn" @click="dataFormSubmit">确 定</el-button>
  	</span>
	</el-dialog>
</template>

<script>
	import { getGroupList,getCategorysList } from '@/api/inspection'
  import { getWaterQuantityList } from '@/api/check'
  import { saveOrUpdateStandard,upgradeStandard } from '@/api/standard'
  import { getSpecialConclusionItemsByStandardId } from '@/api/item'
  import {isNumber} from '@/common/js/validate'
  import {isBlank} from '@/common/js/utils'
	import {CODE_OK} from '@/common/js/config'
  import StandardTableEdit from './standard-table-edit'
	export default {
		data() {
			return {
				visible: false,
				addItemVisible: false,
        addItemSpecialVisible: false,
				groupList: [],
				categoryList: [],
				dataForm: {
					insideInspectionStandardNum: "",
					productGmpLike: "",
					nationInspectionStandardNum: "",
          accordingToInside: "",
          accordingToNation: "",
          usable: 0,
          isWaitTakeEffect: 1,
          specialValue: null,
          createUserId: "",
          createTime: "",
					items: [],
          specialItems: []
				},
				items: [],
        specialItems: [],
        waterQuantityList: [],
        newItem: {
        	inspectionItemId: '',
					inspectionItemName: '',
          printItemName: '',
					insideInspectionItemValue: '',
          haveFormulaValue: 1,
          isNumber: 1,
          isHalfYearTest: 1,
					insideInspectionItemUpValue: '',
					insideInspectionItemDownValue: '',
					insideInspectionItemValueUnit: '',
					nationInspectionItemValue: '',
					inspectionGroupId: '',
					inspectionGroupName: '',
					inspectionCategoryId: '',
					inspectionCategoryName: '',
          printValue: 0,
          isPrint: 0,
          printConfig: 0,
          manHourOne: '',
          manHourTwo: '',
          waterQuantityId: null,
          waterQuantityName: null,
          orderId: 0,
          groupNum: '',
          edit: true
        },
        newItemSpecial: {
          specialItemCategory: null,
          specialItemName: null,
          specialItemValue: null,
          specialItemConclusion: null
        },
        nationInspectionItemValueShow: '',
        insideInspectionItemValueShow: '',
        span1: "<span style='font-size:9px'>",
        span2: "</span>",
        unwatchNation: '',
        unwatchInside: '',
        isUpgrade: false,
        invalidateBtnVisible: false,
        tableEditVisible: false,
        grandDataForm: {},
        isUpdate: false
			}
		},
    components: { StandardTableEdit },
    computed: {
      isZpOrCp() {
        return this.dataForm.insideInspectionStandardNum.trim().substring(0,2) == 'ZP'
         || this.dataForm.insideInspectionStandardNum.trim().substring(0,2) == 'CP'
      },
      isZp() {
        return this.dataForm.insideInspectionStandardNum.trim().substring(0,2) == 'ZP'
      },
      showSpecialItems() {
        return !isBlank(this.dataForm.specialValue)
      }
    },
		props: ["childDataForm"],
		created() {
			if (!isBlank(this.childDataForm)) {
        if (!isBlank(this.childDataForm.oldId)) {
          this.isUpgrade = true
          this.upgradeInit()
        }else{
          this.addOrUpdateInit()
        }
			}
			this.getStandardInfo()
			this.visible = true
		},
		methods: {
      getItems(list) {
        this.items = list.map(v => {
          // 中间品的微生物限度设置
          if (this.isZp && !isBlank(v.inspectionItemName) && v.inspectionItemName.indexOf('微生物限度') > -1) {
            v.isNumber = 0
            v.insideInspectionItemValueUnit = 'cfu/100ml'
          }
          return v
        })
        this.itemSort()
      },
      closeInnerTable(val) {
        this.grandDataForm = ''
        this.tableEditVisible = false
      },
      tableEdit() {
        // 去掉化学式
        let arr = this.items.map(v => {
          if (!isBlank(v.insideInspectionItemValue)) {
            v.insideInspectionItemValue = v.insideInspectionItemValue.replace(new RegExp(this.span1, "gm"),"").replace(new RegExp(this.span2, "gm"),"")
          }
          if (!isBlank(v.nationInspectionItemValue)) {
            v.nationInspectionItemValue = v.nationInspectionItemValue.replace(new RegExp(this.span1, "gm"),"").replace(new RegExp(this.span2, "gm"),"")
          }
          return v
        })
        this.grandDataForm = {
          isUpdate: this.isUpdate,
          list: JSON.parse(JSON.stringify(arr))
        }
        this.tableEditVisible = true
      },
      resetTet() {
        // 如果使当前标准失效，那么清空待生效时间和批号，重新输入
        if (this.invalidateBtnVisible && this.dataForm.isWaitTakeEffect == 0) {
          this.dataForm.takeEffectTime = null
          this.dataForm.effectiveBatchNum = null
        }
      },
      //规定6位数字
      setEffectiveBatchNum() {
        if (!/^\d{6}$/.test(this.dataForm.effectiveBatchNum)) {
          this.$message.error('只允许6位数字')
          this.dataForm.effectiveBatchNum = null
        }
      },
      setWq() {
        if (!isBlank(this.newItem.waterQuantityId)) {
          this.newItem.waterQuantityName = this.waterQuantityList.find((v) => (v.waterQuantityId == this.newItem.waterQuantityId)).waterQuantityName
        }else{
          this.newItem.waterQuantityName = null
        }
      },
      upgradeInit() {
        this.childDataForm.dataForm.insideInspectionStandardId = null
        this.childDataForm.dataForm.isWaitTakeEffect = 0
        this.childDataForm.dataForm.takeEffectTime = null
        this.childDataForm.dataForm.effectiveBatchNum = null
        this.dataForm = this.childDataForm.dataForm
        this.items = this.childDataForm.dataForm.items.map(item => {
          item.parentId = item.inspectionItemId
          item.inspectionItemId = null
          item.show = null
          item.createUserId = null
          item.createTime = null
          item.updateUserId = null
          item.updateTime = null
          this.$set(item,'edit',false)
          // 中间品的微生物限度设置
          if (this.isZp && !isBlank(item.inspectionItemName) && item.inspectionItemName.indexOf('微生物限度') > -1) {
            item.isNumber = 0
            item.insideInspectionItemValueUnit = 'cfu/100ml'
          }
          return item
        })
        this.itemSort()
        this.specialInit(this.childDataForm.oldId)
      },
      addOrUpdateInit() {
        this.dataForm = JSON.parse(JSON.stringify(this.childDataForm))
        // 判断新增或修改
        if (!isBlank(this.dataForm.insideInspectionStandardId)) {
          this.isUpdate = true
        }
        // 如果不是升级，已生效，可用，修改，那么为修改升级的新标准
        if (!this.isUpgrade && this.dataForm.isWaitTakeEffect == 1
          && this.dataForm.usable == 0 && !isBlank(this.dataForm.insideInspectionStandardId)) {
          this.invalidateBtnVisible = true
        }
        this.items = this.childDataForm.items.map(item => {
          this.$set(item,'edit',false)
          // 中间品的微生物限度设置
          if (this.isZp && !isBlank(item.inspectionItemName) && item.inspectionItemName.indexOf('微生物限度') > -1) {
            item.isNumber = 0
            item.insideInspectionItemValueUnit = 'cfu/100ml'
          }
          return item
        })
        this.itemSort()
        this.specialInit(this.dataForm.insideInspectionStandardId)
      },
      itemSort() {
        this.items = this.items.sort((a, b) => {
          return a.orderId - b.orderId
        })
      },
      specialInit(standardId) {
        if (!isBlank(this.dataForm.specialValue)) {
          getSpecialConclusionItemsByStandardId(standardId).then(res => {
            if (res.code === CODE_OK) {
              this.specialItems = res.items.map(item => {
                this.$set(item,'edit',false)
                return item
              })
            }else{
              this.$message.error(res.msg)
            }
          })
        }
      },
      resetFormulaSpec(insideOrNation) {
        if (insideOrNation == 'inside') {
          this.newItem.insideInspectionItemValue = JSON.parse(JSON.stringify(this.insideInspectionItemValueShow))
        }else if(insideOrNation == 'nation'){
          this.newItem.nationInspectionItemValue = JSON.parse(JSON.stringify(this.nationInspectionItemValueShow))
        }else{
          this.newItem.insideInspectionItemValue = JSON.parse(JSON.stringify(this.insideInspectionItemValueShow))
          this.newItem.nationInspectionItemValue = JSON.parse(JSON.stringify(this.nationInspectionItemValueShow))
        }
      },
      selectFormula(insideOrNation) {
        let content = window.getSelection().toString()
        if (content != ''){
          let len = content.length
          let mark = insideOrNation == 'inside' ? this.newItem.insideInspectionItemValue : this.newItem.nationInspectionItemValue
          let position1 = mark.indexOf(content)
          let position2 = position1 + len
          let tempstr1 = mark.substring(0,position1)
          let tempstr2 = mark.substring(position2)
          let contentArr = content.split("")
          let sb = ""
          for (let i=0,j=contentArr.length; i<j; i++) {
            let str = contentArr[i]
            if (isNumber(str)){
              str = this.span1 + str + this.span2
            }
            sb = sb + str
          }
          mark = tempstr1 + sb + tempstr2
          if (insideOrNation == 'inside') {
            this.newItem.insideInspectionItemValue = mark
          }else{
            this.newItem.nationInspectionItemValue = mark
          }
        }
      },
			getStandardInfo () {
        const params = {
          page: 1,
          limit: 9999999
        }
				getGroupList(params).then(res => {
					if (res.code === CODE_OK) {
						this.groupList = res.page.list.filter(g => {
              return g.inspectionGroupId != 1 && g.inspectionGroupId != 2
            })
					}else{
            this.$message.error(res.msg)
          }
				})
				getCategorysList(params).then(res => {
					if (res.code === CODE_OK) {
						this.categoryList = res.page.list
					}else{
            this.$message.error(res.msg)
          }
				})
        getWaterQuantityList().then(res => {
          if (res.code === CODE_OK) {
            this.waterQuantityList = res.list
          }else{
            this.$message.error(res.msg)
          }
        })
			},
			dataFormSubmit() {
        if (this.dataFormVerify()) {
          this.dataForm.items = this.items
          if (!isBlank(this.dataForm.specialValue)) {
            this.dataForm.specialItems = this.specialItems
          }
          if (this.isUpgrade) {
            upgradeStandard(this.dataForm).then(res => {
            	if (res.code === CODE_OK) {
            		this.$message.success('操作成功')
            		this.close('submit')
            	}else{
                this.$message.error(res.msg)
              }
            })
          }else{
            saveOrUpdateStandard(this.dataForm).then(res => {
            	if (res.code === CODE_OK) {
            		this.$message.success('操作成功')
                this.close('submit')
            	}else{
                this.$message.error(res.msg)
              }
            })
          }
        }
			},
      dataFormVerify() {
        if (isBlank(this.items)) {
        	this.$message.error("检验项至少有一个")
        	return false
        }
        let itemNames = []
        for (let i = 0,j = this.items.length; i < j; i++) {
          let n
          if (itemNames.indexOf(n = JSON.parse(JSON.stringify(this.items[i].inspectionItemName))) > -1) {
            this.$message.error("为保证检验台帐质量,检验项名称不能重复,请修改："+n)
            return false
          }
          itemNames.push(n)
        }
        //中间品和成品两个判定生效条件(二选一)，其他只有时间(必选)
        if (this.dataForm.isWaitTakeEffect == 0) {
          if (this.isZpOrCp) {
            if (isBlank(this.dataForm.takeEffectTime) && isBlank(this.dataForm.effectiveBatchNum)) {
              this.$message.error("至少需要一个待生效时间或批号")
              return false
            }
          }else{
            if (isBlank(this.dataForm.takeEffectTime)) {
              this.$message.error("等待生效的时间不能为空")
              return false
            }
          }
        }
        return true
      },
			close(val) {
				this.$emit('close')
        if (val == 'submit') {
          this.$emit('refreshData')
        }
				this.visible = false
			},
			handleClose(itemIndex) {
        this.items.splice(itemIndex, 1)
     	},
      handleCloseSpecial(itemIndex) {
        this.specialItems.splice(itemIndex, 1)
      },
      editItem(itemIndex) {
      	if (itemIndex != 'add') {
          this.$set(this.items[itemIndex],'edit',true)
      		this.newItem = JSON.parse(JSON.stringify(this.items[itemIndex]))
      	}else{
      		this.nationInspectionItemValueShow = ''
          this.insideInspectionItemValueShow = ''
      	}
        let span1 = this.span1
        let span2 = this.span2
        if (!isBlank(this.newItem.insideInspectionItemValue)){
          this.insideInspectionItemValueShow = this.newItem.insideInspectionItemValue.replace(new RegExp(span1, "gm"),"").replace(new RegExp(span2, "gm"),"")
        }
        this.unwatchInside = this.$watch('insideInspectionItemValueShow', _ => { this.newItem.insideInspectionItemValue = this.insideInspectionItemValueShow })
        if (!isBlank(this.newItem.nationInspectionItemValue)){
          this.nationInspectionItemValueShow = this.newItem.nationInspectionItemValue.replace(new RegExp(span1, "gm"),"").replace(new RegExp(span2, "gm"),"")
        }
        this.unwatchNation = this.$watch('nationInspectionItemValueShow', _ => { this.newItem.nationInspectionItemValue = this.nationInspectionItemValueShow })
      	this.addItemVisible = true
      },
      editItemSpecial(itemIndex) {
      	if (itemIndex != 'add') {
          this.$set(this.specialItems[itemIndex],'edit',true)
      		this.newItemSpecial = JSON.parse(JSON.stringify(this.specialItems[itemIndex]))
      	}
        this.addItemSpecialVisible = true
      },
      closeInner() {
        this.unwatchInside()
        this.unwatchNation()
        this.nationInspectionItemValueShow = null
        this.insideInspectionItemValueShow = null
      	this.newItem = {
        	inspectionItemId: '',
        	inspectionItemName: '',
        	printItemName: '',
        	insideInspectionItemValue: '',
        	haveFormulaValue: 1,
        	isNumber: 1,
          isHalfYearTest: 1,
        	insideInspectionItemUpValue: '',
        	insideInspectionItemDownValue: '',
        	insideInspectionItemValueUnit: '',
        	nationInspectionItemValue: '',
        	inspectionGroupId: '',
        	inspectionGroupName: '',
        	inspectionCategoryId: '',
        	inspectionCategoryName: '',
        	printValue: 0,
        	isPrint: 0,
        	printConfig: 0,
        	manHourOne: '',
        	manHourTwo: '',
        	orderId: 0,
          edit: true
        }
        this.items.map(item => {
          item.edit = false
          return item
        })
        this.addItemVisible = false
      },
      closeInnerSpecial() {
        this.newItemSpecial = {
          specialItemCategory: null,
          specialItemName: null,
          specialItemValue: null,
          specialItemConclusion: null
        }
        this.specialItems.map(item => {
          item.edit = false
          return item
        })
        this.addItemSpecialVisible = false
      },
      newItemSubmit() {
        if (this.newItemVerify()) {
          this.newItem.edit = false
          let itemIndex = this.items.findIndex(item => item.edit)
          if (itemIndex == -1) {
          	this.items.push(JSON.parse(JSON.stringify(this.newItem)))
          }else{
            this.items[itemIndex] = JSON.parse(JSON.stringify(this.newItem))
          }
          this.closeInner()
        }
      },
      newItemSpecialSubmit() {
        if (this.newItemSpecialVerify()) {
          this.newItemSpecial.edit = false
          let itemIndex = this.specialItems.findIndex(item => item.edit)
          if (itemIndex == -1) {
          	this.specialItems.push(JSON.parse(JSON.stringify(this.newItemSpecial)))
          }else{
            this.specialItems[itemIndex] = JSON.parse(JSON.stringify(this.newItemSpecial))
          }
          this.closeInnerSpecial()
        }
      },
      newItemVerify() {
        //检验项目名称，所属小组，所属分类
        let e = this.newItem,en
        if (isBlank(en = e.inspectionItemName)) {
          this.$message.error('检验项目名称不能为空')
          return false
        }
        if (this.items.filter(item => !item.edit).map(item => { return item.inspectionItemName }).indexOf(en) > -1) {
          this.$message.error('为了检验台帐质量,检验项目名称不能重复,该标准已经有“'+en+'”了')
          return false
        }
        if (isBlank(e.inspectionGroupId)) {
          this.$message.error('所属小组不能为空')
          return false
        }
        if (isBlank(e.inspectionCategoryId)) {
          this.$message.error('所属分类不能为空')
          return false
        }
        return true
      },
      newItemSpecialVerify() {
        let e = this.newItemSpecial
        if (isBlank(e.specialItemCategory)) {
          this.$message.error('额外的检验项目分类不能为空')
          return false
        }
        if (isBlank(e.specialItemName)) {
          this.$message.error('额外的检验项目名称不能为空')
          return false
        }
        if (isBlank(e.specialItemValue)) {
          this.$message.error('额外的检验项目检验标准不能为空')
          return false
        }
        if (isBlank(e.specialItemConclusion)) {
          this.$message.error('额外的检验项目内置检验结果不能为空')
          return false
        }
        return true
      },
      setPrintItemName() {
        //中文括号换英文括号
        this.newItem.inspectionItemName = this.newItem.inspectionItemName.replace(/（/g,'(')
        this.newItem.inspectionItemName = this.newItem.inspectionItemName.replace(/）/g,')')
        this.newItem.printItemName = JSON.parse(JSON.stringify(this.newItem.inspectionItemName))
        // 中间品的微生物限度设置
        if (this.isZp && !isBlank(this.newItem.inspectionItemName) && this.newItem.inspectionItemName.indexOf('微生物限度') > -1) {
          this.newItem.isNumber = 0
          this.newItem.insideInspectionItemValueUnit = 'cfu/100ml'
        }
      }
    }
	}
</script>

<style scoped>
	.el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  >>> .el-form-item__content {
    line-height: 40px !important;
  }
  .old-num {
    font-size: 20px;
    text-align: center;
    color: #F56C6C;
  }
  .new-num{
    font-size: 20px;
    text-align: center;
    color: #F56C6C;
  }
  .to {
    font-size: 48px;
    text-align: center;
    color: #409EFF;
  }
</style>
